import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/app/prisma/client';
import getUserId from '@/utils/getUserId';
import { FriendStatus } from '@/app/types/friend';

export async function GET(req: NextRequest) {
  try {
    const userId = getUserId(req);
    if (!userId) {
      return NextResponse.json({ error: '请先登录' }, { status: 401 });
    }

    const requests = await prisma.friend.findMany({
      where: {
        friendId: userId,
        status: FriendStatus.PENDING,
      },
      include: {
        user: {
          select: {
            id: true,
            username: true,
            nickname: true,
            avatar: true,
            bio: true,
          },
        },
      },
      orderBy: {
        createdAt: 'desc',
      },
    });

    const friendRequests = requests.map((request) => ({
        requestId: request.id,
        sender: request.user,
        createdAt: request.createdAt,
    }));

    return NextResponse.json(friendRequests);
  } catch (error) {
    console.error('获取好友请求列表失败:', error);
    return NextResponse.json({ error: '服务器错误' }, { status: 500 });
  }
} 